﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework.Constraints;
using Simhopp.Model;

namespace NUnit.Framework.Tests
{
    [TestFixture]
    class TestOfWholeCompetition
    {
        bool equal, equal2, equal3, CompetitionFinished;
        Competition comp;
        Diver d1, d2, d3, d4, d5;
        Judge j1, j2, j3, j4, j5;
        [SetUp]
        public void Init()
        {
            equal = equal2 = CompetitionFinished = false;
            equal3 = true;
            comp = new Competition(1,"VM-kval",CompetitionType.International,"Sverige",new DateTime(2014, 2, 17),Height.Platform_10m,AgeClass.B,'M',5,3);

            comp.AddJudge(new Judge(1, "Boris", Countries.Russia, "pass12", "Bor"));
            comp.AddJudge(new Judge(2, "Björn", Countries.Croatia, "pass12", "Bor"));
            comp.AddJudge(new Judge(3, "Benny", Countries.Uganda, "pass12", "Bor"));
            comp.AddJudge(new Judge(4, "Betty", Countries.Rwanda, "pass12", "Bor"));
            comp.AddJudge(new Judge(5, "Borat", Countries.East_Timor_or_Timur, "pass12", "Bor"));

            comp.AddDiver(new Diver(1, "Alfons", new DateTime(),'M',1, 0, Countries.Switzerland));
            comp.AddDiver(new Diver(2, "Albert", new DateTime(), 'M', 2, 0, Countries.Syria));
            comp.AddDiver(new Diver(3, "Alma", new DateTime(), 'M', 3, 0, Countries.Swaziland));
            comp.AddDiver(new Diver(4, "Amanda", new DateTime(), 'M', 4, 0, Countries.Sverige));
            comp.AddDiver(new Diver(5, "Anita", new DateTime(), 'M', 5, 0, Countries.Thailand));

            comp.CompetingDivers = comp.DiverList.Count();

            j1 = comp.GetJudge(1);
            j2 = comp.GetJudge(2);
            j3 = comp.GetJudge(3);
            j4 = comp.GetJudge(4);
            j5 = comp.GetJudge(5);

            d1 = comp.GetDiver(1);
            d2 = comp.GetDiver(2);
            d3 = comp.GetDiver(3);
            d4 = comp.GetDiver(4);
            d5 = comp.GetDiver(5);

            d1.AddDive(1, "Volt", 2.5, "AB101", 2);
            d1.AddDive(2, "VoltSkruv", 3.5, "A102", 1);
            d1.AddDive(3, "SkruvVolt", 1.5, "E104", 3);

            d2.AddDive(1, "Volt", 2.5, "AB101", 3);
            d2.AddDive(2, "VoltSkruv", 3.5, "A102", 2);
            d2.AddDive(3, "SkruvVolt", 1.5, "E104", 1);

            d3.AddDive(1, "Volt", 2.5, "AB101", 1);
            d3.AddDive(2, "VoltSkruv", 3.5, "A102", 2);
            d3.AddDive(3, "SkruvVolt", 1.5, "E104", 3);

            d4.AddDive(1, "Volt", 2.5, "AB101", 2);
            d4.AddDive(2, "VoltSkruv", 3.5, "A102", 3);
            d4.AddDive(3, "SkruvVolt", 1.5, "E104", 1);

            d5.AddDive(1, "Volt", 2.5, "AB101", 3);
            d5.AddDive(2, "VoltSkruv", 3.5, "A102", 2);
            d5.AddDive(3, "SkruvVolt", 1.5, "E104", 1);
           
           
        }
        /// <summary>
        /// Tests a whole competition, checking that the calculations for totalpoints and judgescoring are correct
        /// and that the roundsystem, including DiverUpNext and Round, works.
        /// </summary>
        [Test]
        public void RunWholeCompetition()
        {
            if (comp.JudgingJudges == 5 && comp.RoundsInCompetition == 3)
                equal = true;

            while (!CompetitionFinished)
            {
                Diver diver = comp.GetDiver(comp.DiverUpNext);
                Dive dive = diver.GetDiveByNumber(comp.Round);
                foreach (Judge j in comp.JudgeList)
                    diver.AddScoreAndJudgeIDToJudgeScoreDict(j.ID, comp.Round, 5);
                dive.CalculateDiveScore();
                comp.DiverUpNext++;
                if (comp.DiverUpNext == comp.CompetingDivers + 1)
                {
                    comp.DiverUpNext = 1;
                    comp.Round++;
                }
                if (comp.Round == comp.RoundsInCompetition + 1)
                    CompetitionFinished = true;
            }

            foreach (Diver diver in comp.DiverList)
                diver.CalculateTotalPoints();

            foreach (Diver diver in comp.DiverList)
            {
                if (diver.TotalPoints != 112.50)
                    equal3 = false;
            }
            if (comp.Round == 4)
                equal2 = true;
            Assert.AreEqual(equal, true);
            Assert.AreEqual(equal2, true);
            Assert.AreEqual(equal3, true);


            Console.WriteLine("Judges:");
            foreach (Judge judge in comp.JudgeList)
            {
                Console.WriteLine(judge.ID + ". " + judge.Name);
            }

            Console.WriteLine("Divers:");
            foreach (Diver diver in comp.DiverList)
            {
                Console.WriteLine(diver.ID + ". " + diver.Name + ". " + diver.TotalPoints);
                foreach (Dive dive in diver.DiveList)
                {
                    Console.WriteLine(dive.ID + ". " + dive.Name);
                    foreach (var judgescore in dive.JudgeScore)
                    {
                        Console.Write(judgescore.Value + ", ");
                    }
                    Console.WriteLine();
            
        }
    }
}
        [TearDown]
        public void Reset()
        {
        }
    }
}
